/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.ip;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
 *
 * @author hxg
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "IpAddressEntry")
public class IpAddressEntry implements Serializable {

    //String ipAddr;
    //String netMask;\
    @XmlElement(type=IpAddress.class)
    IpAddress ipAddress;
    @XmlElement
    String ipAddrIfIndex;
    final public static String IpAddr = "1.3.6.1.2.1.4.20.1.1";
    final public static String NetMask = "1.3.6.1.2.1.4.20.1.3";
    final public static String IpAddrIfIndex = "1.3.6.1.2.1.4.20.1.2";

    public IpAddressEntry() {
    }

    public IpAddressEntry(String ip, String mark) {
        ipAddress = new IpAddress(ip, mark);
    }

    public IpAddressEntry(String ip, int mark) {
        ipAddress = new IpAddress(ip, mark);
    }

    public String getIp() {
        return ipAddress.getIpAddr();
    }

    public IpAddress getIpAddr() {
        return ipAddress;
    }

    public String getIpAddrIfIndex() {
        return ipAddrIfIndex;
    }

    public void setIpAddrIfIndex(String ipAddrIfIndex) {
        this.ipAddrIfIndex = ipAddrIfIndex;
    }

    public String getNetMask() {
        return ipAddress.getNetMaskString();
    }

    public int getNetMaskInt() {
        return ipAddress.getNetMask();
    }

    public static int getMaskTrans(String mask) {
        try {
            String[] ips = mask.split("[.]");
            int ipnum = Integer.parseInt(ips[0]) * 256 * 65536 + Integer.parseInt(ips[1]) * 65536 + Integer.parseInt(ips[2]) * 256 + Integer.parseInt(ips[3]);
            String netmask = Integer.toBinaryString(ipnum);
            int intm = -1;
            if (netmask.contains("0")) {
                intm = netmask.indexOf('0');
            } else {
                intm = 32;
            }
            return intm;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public String toString() {
        return ipAddress.toString();
    }
}
